package com.rt.easyjava.base.biz1.service.impl.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.easyjava.base.biz1.bean.TmptTbColumnBiz;
import com.rt.easyjava.base.biz1.dto.TmptTbColumnBizDto;
import com.rt.easyjava.base.biz1.mapper.TmptTbColumnBizMapper;
import com.rt.easyjava.base.biz1.service.intf.base.TmptTbColumnService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-07-04
 */
@Slf4j
public class TmptTbColumnServiceImpl extends ServiceImpl<TmptTbColumnBizMapper, TmptTbColumnBiz> implements TmptTbColumnService {
    @Autowired
    protected MyMapper mapper;
    @Resource
    protected TmptTbColumnBizMapper tmptTbColumnBizMapper;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
    * create a object
    * @param tmptTbColumnBizDto
    * @return
    */
    @Override
    public Result<TmptTbColumnBizDto> create(TmptTbColumnBizDto tmptTbColumnBizDto) {
        Result<TmptTbColumnBizDto> result = new Result<>();

        // dto to entity
        TmptTbColumnBiz tmptTbColumnBiz = mapper.map(tmptTbColumnBizDto, TmptTbColumnBiz.class);

        tmptTbColumnBizMapper.insert(tmptTbColumnBiz);

        return result.success();
    }

    /**
    * update a object by Id
    * @param tmptTbColumnBizDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<TmptTbColumnBizDto> update(TmptTbColumnBizDto tmptTbColumnBizDto, UpdateWrapper updateWrapper) {
        Result<TmptTbColumnBizDto> result = new Result<>();

        // dto to entity
        TmptTbColumnBiz tmptTbColumnBiz = mapper.map(tmptTbColumnBizDto, TmptTbColumnBiz.class);

        tmptTbColumnBizMapper.update(tmptTbColumnBiz, updateWrapper);

        return result.success();
    }

    /**
    * 分页 (无合计功能)
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<TmptTbColumnBizDto> page(Page<TmptTbColumnBiz> page, QueryWrapper<TmptTbColumnBiz> queryWrapper){
        Result<TmptTbColumnBizDto> result = new Result<>();

        // mapper
        IPage<TmptTbColumnBiz> iPage = tmptTbColumnBizMapper.selectPage(page, queryWrapper);

        // 查询结果转换成DTO
        List<TmptTbColumnBizDto> tmptTbColumnBizDtoList= mapper.maplist(iPage.getRecords(), TmptTbColumnBizDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptTbColumnBizDtoList);

        return result.success();
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptTbColumnBizDto> selectBy(QueryWrapper<TmptTbColumnBiz> queryWrapper) {
        Result<TmptTbColumnBizDto> result = new Result<>();

        List<TmptTbColumnBiz> list = tmptTbColumnBizMapper.selectList(queryWrapper);

        List<TmptTbColumnBizDto> dtoList = mapper.maplist(list, TmptTbColumnBizDto.class);

        return result.setList(dtoList).success();
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<TmptTbColumnBizDto> selectById(Long id) {
        Result<TmptTbColumnBizDto> result = new Result<>();

        TmptTbColumnBiz tmptTbColumnBiz = tmptTbColumnBizMapper.selectById(id);

        TmptTbColumnBizDto tmptTbColumnBizDto = mapper.map(tmptTbColumnBiz, TmptTbColumnBizDto.class);

        return result.setObj(tmptTbColumnBizDto).success();
    }



    /**
    * select dto list by key
    * @param columnName
    * @param key
    * @return
    */
    @Override
    public Result<TmptTbColumnBizDto> selectDtoListByKey(String columnName, String key) {
        Result<TmptTbColumnBizDto> result = new Result<>();

        List<TmptTbColumnBiz> tmptTbColumnBizList = selectListByKey(columnName, key);
        if (tmptTbColumnBizList == null) {
            return result.fault("no data by key:"+key);
        }

        List<TmptTbColumnBizDto> tmptTbColumnBizDtoList = mapper.maplist(tmptTbColumnBizList, TmptTbColumnBizDto.class);

        return result.setList(tmptTbColumnBizDtoList).success();
    }

    /**
    * select bean by key
    * @param columnName
    * @param key
    * @return
    */
    @Override
    public TmptTbColumnBiz selectByKey(String columnName, String key) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(columnName, key);
        queryWrapper.eq("deleteflag", 0);
        List<TmptTbColumnBiz> eventRuleBizList = tmptTbColumnBizMapper.selectList(queryWrapper);
        if (eventRuleBizList.size() == 0) {
            return null;
        }
        return eventRuleBizList.get(0);
    }

    /**
    * select list bean by key
    * @param columnName
    * @param key
    * @return
    */
    @Override
    public List<TmptTbColumnBiz> selectListByKey(String columnName, String key) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(columnName, key);
        queryWrapper.eq("deleteflag", 0);
        List<TmptTbColumnBiz> eventRuleBizList = tmptTbColumnBizMapper.selectList(queryWrapper);
        return eventRuleBizList;
    }






    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * 默认开启合计查询，按需注掉代码
    * @param page
    * @param queryWrapper: 一定要是这个代理工具类才行：QueryWrapper queryWrapper = MyQueryWrapper.buildQueryWrapper(...)
    * @return
    */
    @Override
    public Result<TmptTbColumnBizDto> myPage(Page<TmptTbColumnBiz> page, QueryWrapper<TmptTbColumnBiz> queryWrapper) {
        Result<TmptTbColumnBizDto> result = new Result();

        IPage<TmptTbColumnBiz> iPage = tmptTbColumnBizMapper.myPage(page, queryWrapper);
        // 查询结果转换成DTO
        List<TmptTbColumnBizDto> tmptTbColumnBizDtoList = mapper.maplist(iPage.getRecords(), TmptTbColumnBizDto.class);
        // 返回结果集
        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptTbColumnBizDtoList);

        // 合计
        List<TmptTbColumnBiz> sumList = tmptTbColumnBizMapper.mySelectSum(queryWrapper);
        List<TmptTbColumnBizDto> sumDtoList = mapper.maplist(sumList, TmptTbColumnBizDto.class);
        result.getMap().put("footer", sumDtoList);

        return result.success();
    }

    /**
    * 查询
    * @param queryWrapper: 一定要是这个代理工具类才行：QueryWrapper queryWrapper = MyQueryWrapper.buildQueryWrapper(...)
    * @return
    */
    @Override
    public Result<TmptTbColumnBizDto> mySelectBy(QueryWrapper<TmptTbColumnBiz> queryWrapper) {
        Result<TmptTbColumnBizDto> result = new Result();

        List<TmptTbColumnBiz> list = tmptTbColumnBizMapper.mySelectBy(queryWrapper);
        List<TmptTbColumnBizDto> dtoList = mapper.maplist(list, TmptTbColumnBizDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<TmptTbColumnBizDto> mySelectById(long id) {
        Result<TmptTbColumnBizDto> result = new Result();

        TmptTbColumnBiz tmptTbColumnBiz = tmptTbColumnBizMapper.mySelectById(id);
        TmptTbColumnBizDto dto = mapper.map(tmptTbColumnBiz,TmptTbColumnBizDto.class);
        result.setObj(dto);

        return result.success();
    }


}
